package br.ufrj.dcc.tcc.android;

import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

	public final static String EXTRA_MESSAGE = "br.ufrj.tcc.metrics.MESSAGE";

	private ListView mainListView;
	private ArrayAdapter<String> listAdapter;

	private AlertDialog dialog;

	private double latitude;
	private double longitude;
	private String timeListResult;
	private String gpsStatus;
	LocationManager locationManager;

	private Date startTimeGps;
	private Date endTimeGps;

	private Date startTimeItemList;
	private Date endTimeItemList;

	NotificationManager notificationManager;


	private LocationListener locationListener;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Date start = new Date();
		// definindo a list
		configureListView();
		Date end = new Date();

		Log.d("TIME_LIST", "" + (end.getTime() - start.getTime()));
		System.out.println("Time= " + (end.getTime() - start.getTime()));

		timeListResult = "Time= " + (end.getTime() - start.getTime());

		// define o location manager
		configureLocationManager();

	}

	private void configureLocationManager() {
		locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

		locationListener = new LocationListener() {

			public void onStatusChanged(String provider, int status, Bundle extras) {
				gpsStatus= provider + " " + extras;
			} 

			public void onProviderEnabled(String provider) {}

			public void onProviderDisabled(String provider) {}

			public void onLocationChanged(Location location) {
				latitude = location.getLatitude();
				longitude = location.getLongitude();
				dialog.setMessage("TimeList= " + timeListResult +
						"\nGPS Status = " + gpsStatus +
						"\nLatitude= " + latitude + 
						"\nLongitude= " + longitude);
			}
		};

		//       LocationProvider provider = locationManager.getProvider(LocationManager.GPS_PROVIDER);

		final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

		if (!gpsEnabled) {
			enableLocationSettings();
		}

		locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

	}

	private void enableLocationSettings() {
		Intent settingsIntent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
		startActivity(settingsIntent);
	}

	public void onClickButtonEvent(View view) {
		configureAlertDialog();
		dialog.show();
	}

	private void configureAlertDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("GPS")
		.setCancelable(true)
		.setMessage("TimeList= " + timeListResult + "\nWaiting for gps position ...")
		.setNeutralButton("Fechar", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		dialog = builder.create();
	}

	private void configureListView() {
		mainListView = (ListView) findViewById(R.id.listitems);

		String[] myStringArray = new String[100];

		for(int i=0; i<myStringArray.length; i++)  {
			myStringArray[i] = "Item " + i;
		}

		//       ListView listView = (ListView) findViewById(R.id.listitens);
		ArrayList<String> arrayList = new ArrayList<String>();

		for(int i=0; i<myStringArray.length; i++)  {
			arrayList.add(myStringArray[i]);
		}

		listAdapter = new ArrayAdapter<String>(this, R.layout.row, arrayList);

		mainListView.setAdapter(listAdapter);

		mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				startTimeItemList = new Date();

				onClickListEvent(view);

			}

		});

	}

	public void onClickListEvent(View view) {
		Intent intent = new Intent(this, ItemList.class);

		//    	intent.putExtra(EXTRA_MESSAGE, ((TextView) view).getText());
		endTimeItemList = new Date();
		intent.putExtra(EXTRA_MESSAGE,  ((TextView) view).getText() + " " + (endTimeItemList.getTime() - startTimeItemList.getTime()));
		startActivity(intent);

	}


}
